package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {


    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询所有护理等级
     * @return
     */
    @Override
    public Object getAllLeval() {
        return nursingLevelMapper.getAllLeval();
    }

    /**
     * 新增
     * @param nursingLevelDto
     */
    @Override
    public void addLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        try {
            nursingLevelMapper.addLevel(nursingLevel);
        } catch (Exception e) {
            throw new RuntimeException("名称重复");
        }
    }

    /**
     * 条件分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> getPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用Mapper方法
        Page<NursingPlanVo> byPage = nursingLevelMapper.getPage(name,status);
        //类型转换
        return PageResponse.of(byPage,NursingLevelVo.class);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Long id) {
        return BeanUtil.toBean(nursingLevelMapper.getById(id),NursingLevelVo.class);
    }

    /**
     * 修改
     * @param nursingLevelDto
     */
    @Override
    public void updateLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.updateLevel(nursingLevel);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delLevel(Long id) {
        nursingLevelMapper.deleteById(id);
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id,status);
    }
}
